package
{
	import flash.desktop.NativeProcess;
	import flash.desktop.NativeProcessStartupInfo;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.events.TimerEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.geom.Rectangle;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	import flash.utils.Timer;
	import flash.utils.setTimeout;
	
	import mx.graphics.codec.JPEGEncoder;
	import mx.graphics.codec.PNGEncoder;
	
	import spark.components.Label;

	public class Png_test
	{
		private static var _ins:Png_test;
		
		public static var exportUrl:String;
		public static var importRoot:String;
		public static var printMsg:Label;
		
		private var _path:String;
		
		private var _urlLoader:URLLoader;
		private var _timer2:Timer;
		
		public static function get ins():Png_test
		{
			if(!_ins){
				_ins = new Png_test();
			}
			return _ins;
		}
		
		public function Png_test()
		{
		}
		public function doLoad(path:String):void
		{
			//"C:\Users\Administrator\Desktop\ani原始\21000\bg_tiles\1.mp"//路径格式是这样的
			
			_path = path;
			
			loadData();
		}
		private function onTimer(e:TimerEvent):void
		{
			var fl:File = new File(_path);
			
			if(fl.exists){
				
				var f:File = new File("d:/pngquant/pngquant.exe");// File.applicationDirectory;
				f = f.resolvePath("d:/pngquant/pngquant.exe");
				var na:NativeProcessStartupInfo = new NativeProcessStartupInfo();//AIR2.0中的新类, 创建进程的信息对象
				na.executable = f;// 将你指定的MyFile对象指定为可执行文件
				var arr:Vector.<String> = new Vector.<String>();
				//				arr.push("D:/pngquant/2010_tx10.ani.png");
				
				var path:String = fl.nativePath.replace(/\\/gi, "\/");
				arr.push(path);
				na.arguments = arr;
				var process:NativeProcess = new NativeProcess;// 创建一个本地进程
				process.start(na);// 运行本地进程
				
				if(!_timer2){
					_timer2 = new Timer(100);
					_timer2.addEventListener(TimerEvent.TIMER, onTimer2);
					_timer2.start();
				}
			}
		}
		private function onTimer2(e:TimerEvent):void
		{
//			var fix:String = key.substring(key.lastIndexOf("."));
//			var path:String = key.replace(fix, "-fs8" + fix);
//			
//			var rootFile:File = new File(curExportUrl);
//			var fl:File = rootFile.resolvePath(path);
//			if(fl.exists && _outPathDic[key] != 2){
//				
//				_outPathDic[key] = 2;
//				
//				var rootFile2:File = new File(curExportUrl);
//				var fl2:File = rootFile2.resolvePath(key);
//				
//				fl.moveTo(fl2, true);
//				//					fl.copyTo(fl2, true);////
//			}
//			
//			var count:int = 0;
//			for each(var value:int in _outPathDic){
//				if(value == 2){
//					count ++;
//				}
//			}
//			
//			if(count >= _ani.length){//说明图片已压缩并剪切好
//				setTimeout(onStartCompress, 500 + Math.random() * 2000);
//				_timer2.stop();
//				_timer2.removeEventListener(TimerEvent.TIMER, onTimer2);
//				_timer.stop();
//				_timer.removeEventListener(TimerEvent.TIMER, onTimer);
//			}
		}
		
		/**
		 * 如果有地图数据，则加载
		 */
		private function loadData():void
		{
			if(_path){
				var request:URLRequest = new URLRequest(_path);
				_urlLoader = new URLLoader();
				_urlLoader.addEventListener(Event.COMPLETE, onComplete1);
				_urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onLoadError1);
				_urlLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, onLoadError2);
				_urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onLoadError2);
				_urlLoader.data = null;
				_urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
				_urlLoader.load(request);
			}
			else{
				//				loadClip();
			}
		}
		private function onComplete1(e:Event):void
		{
			_urlLoader.removeEventListener(Event.COMPLETE, onComplete1);
			_urlLoader.removeEventListener(IOErrorEvent.IO_ERROR, onLoadError1);
			_urlLoader.removeEventListener(HTTPStatusEvent.HTTP_STATUS, onLoadError2);
			_urlLoader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, onLoadError2);
			
			var bytes:ByteArray = (_urlLoader.data);
//			parser(bytes);
			
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onBytesLoaded);
			loader.loadBytes(bytes);
			
		}
		private function onBytesLoaded(e:Event):void 
		{
			var bitmap:Bitmap = e.target.content as Bitmap;
			e.target.loader.contentLoaderInfo.removeEventListener (Event.COMPLETE, onBytesLoaded);
			
			writeImg(bitmap.bitmapData, "wqwqwqwqwq.png");
		} 
		public function writeImg(param1:BitmapData, path:String):void
		{
			//			var path:String = _path.replace(importRoot, "");
			//			if(path.indexOf("\\") == 0){
			//				path = path.substring(1);
			//			}
			
			var imgByteArray:ByteArray;
			var jpgenc:JPEGEncoder;
			var pngenc:PNGEncoder;
			var msg:Label;
			var bmd:BitmapData = param1;
			var laststr:String;
			
			pngenc = new PNGEncoder();
			imgByteArray = new ByteArray();
			imgByteArray.writeUnsignedInt(712);
			imgByteArray.writeBytes(bmd.getPixels(new Rectangle(0, 0, bmd.width, bmd.height)));//pngenc.encode(bmd);
			imgByteArray.position = 0;
			imgByteArray.compress();
			
			var rootFile:File = new File(curExportUrl);
			var fl:File = rootFile.resolvePath(path);
			var fs:FileStream = new FileStream();
			
			try
			{
				fs.open(fl, FileMode.WRITE);
				fs.writeBytes(imgByteArray);
				fs.close();
			}
			catch(e:Error)
			{
				Png_test.printMsg.text = "打包出错：" + fl.nativePath;
//				throw new Error("打包出错：" + path);
			}
		}
		private function get curExportUrl():String
		{
			var p:String = _path.replace(importRoot, "");
			var index:int = p.lastIndexOf("\\");
			if(index != -1){
				p = p.substring(0, index);
			}
			return exportUrl + p;
		}
		
		private function onLoadError1(e:Event):void
		{
			trace("加载是失败");
			//			loadClip();
		}
		private function onLoadError2(e:Event):void
		{
			trace("加载是失败2");
		}
	}
}